package com.loongtech.bi.action.biplayer;

import com.loongtech.bi.action.BaseBiSearchAction;
import com.loongtech.bi.manager.player.ActivePlayerManager;
import com.loongtech.bi.manager.player.ActivePlayerManager.ActiveTendData;
import com.loongtech.bi.support.chart.ChartTool;
import com.loongtech.bi.support.chart.ChartType;
import com.loongtech.bi.support.chart.ChartUIData;
import com.loongtech.core.util.Time;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 玩家分析-活跃分析Action
 *
 * @author Marin
 * @date 2015年09月17日
 */
@Controller
@Scope("session")
@RequestMapping("/player/activePlayer")
public class ActivePlayerBiAction extends BaseBiSearchAction {
	private static final long serialVersionUID = 1L;
    private List<ActiveTendData> activeTrendDatas;  // 前端要显示的数据1
    private ChartUIData activeTrendChart;  // 前端要显示的柱状图1数据
    private ChartUIData activeTrendChart1;  // 前端要显示的柱状图1数据
    private ChartUIData activeTrendChart2;  // 前端要显示的柱状图1数据

    @Autowired
    private ActivePlayerManager activePlayerManager;

    @RequestMapping(value = "initParam.do", method = {RequestMethod.GET, RequestMethod.POST})
    public void initParam(HttpServletRequest request, HttpServletResponse response, String callback) {
        Map<String, Object> result = new HashMap<>();
        try {
            result.put("res", "0");
            result.put("serverItmes", serverItmes);
            result.put("channelItmes", channelItmes);
            result.put("platformItmes", platformItmes);
            printToJson(response, result, callback);
        } catch (Exception e) {
            e.printStackTrace();
            result.put("res", "1");
            result.put("msg", e.getMessage());
            printToJson(response, result, callback);
            return;
        }
    }

    @RequestMapping(value = "search.do", method = {RequestMethod.GET, RequestMethod.POST})
    public void search(HttpServletRequest request, HttpServletResponse response, @RequestParam String dstart, @RequestParam String dend, @RequestParam String platform, @RequestParam String channel, @RequestParam String server, String callback) {
        Map<String, Object> result = new HashMap<>();
        try {
            dateStart = Time.formatDate(dstart, Time.TIMEPATTERN_SIMPLE);
            dateEnd = Time.formatDate(dend, Time.TIMEPATTERN_SIMPLE);
            queryPlatform = platform;
            queryChannel = channel;
            queryServer = server;

            activeTrendDatas = activePlayerManager.getActiveTendData(dateStart, dateEnd, platform, channel, server);
            if (activeTrendDatas != null && activeTrendDatas.size() > 0) {
                activeTrendChart = new ChartUIData();
                activeTrendChart.setJsonType(ChartType.SPLINE.type);
                activeTrendChart.setJsonStr(new ChartTool(activeTrendDatas, ActiveTendData.class, "dau").loadData(activeTrendChart.getJsonType()));
                //下拉框
                activeTrendChart.setAllItmes(ChartTool.getAllItems(ActiveTendData.class));

                activeTrendChart1 = new ChartUIData();
                activeTrendChart1.setJsonType(ChartType.SPLINE.type);
                activeTrendChart1.setJsonStr(new ChartTool(activeTrendDatas, ActiveTendData.class, "newUser").loadData(activeTrendChart1.getJsonType()));
                //下拉框
                activeTrendChart1.setAllItmes(ChartTool.getAllItems(ActiveTendData.class));

                activeTrendChart2 = new ChartUIData();
                activeTrendChart2.setJsonType(ChartType.SPLINE.type);
                activeTrendChart2.setJsonStr(new ChartTool(activeTrendDatas, ActiveTendData.class, "activeOldUser").loadData(activeTrendChart2.getJsonType()));
                //下拉框
                activeTrendChart2.setAllItmes(ChartTool.getAllItems(ActiveTendData.class));
            } else {
                activeTrendChart = null;
                activeTrendChart1 = null;
                activeTrendChart2 = null;
            }

            result.put("res", "0");
            result.put("activeTrendDatas", activeTrendDatas);
            result.put("activeTrendChart", activeTrendChart);
            result.put("activeTrendChart1", activeTrendChart1);
            result.put("activeTrendChart2", activeTrendChart2);

            printToJson(response, result, callback);
        } catch (Exception e) {
            e.printStackTrace();
            result.put("res", "1");
            result.put("msg", e.getMessage());
            printToJson(response, result, callback);
        }
    }

    //趋势图表的选择
    @RequestMapping(value = "updateActiveType.do", method = {RequestMethod.GET, RequestMethod.POST})
    public void updateActiveType(HttpServletRequest request, HttpServletResponse response, @RequestParam String activeType, String callback) {
        Map<String, Object> result = new HashMap<>();
        try {
            activeTrendChart.setJsonStr(new ChartTool(activeTrendDatas, ActiveTendData.class, activeType).loadData(activeTrendChart.getJsonType()));

            result.put("res", "0");
            result.put("activeTrendChart", activeTrendChart);

            printToJson(response, result, callback);
        } catch (Exception e) {
            e.printStackTrace();
            result.put("res", "1");
            result.put("msg", e.getMessage());
            printToJson(response, result, callback);
        }
    }

    @RequestMapping(value = "updateServer.do", method = {RequestMethod.GET, RequestMethod.POST})
    public void updateServer(HttpServletRequest request, HttpServletResponse response, String server, String callback) {
        try {
            queryServer = server;
            selectedServer();
            Map<String, Object> result = new HashMap<>();
            result.put("res", "0");
            result.put("channelItmes", channelItmes);
            result.put("platformItmes", platformItmes);
            printToJson(response, result, callback);
        } catch (Exception e) {
            e.printStackTrace();
            Map<String, Object> result = new HashMap<String, Object>();
            result.put("res", "1");
            result.put("msg", e.getMessage());
            printToJson(response, result, callback);
            return;
        }
    }

    @RequestMapping(value = "updateChannel.do", method = {RequestMethod.GET, RequestMethod.POST})
    public void updateChannel(HttpServletRequest request, HttpServletResponse response, String channel, String callback) {
        try {
            queryChannel = channel;
            updateServerItmes();
            Map<String, Object> result = new HashMap<>();
            result.put("res", "0");
            result.put("serverItmes", serverItmes);
            result.put("platformItmes", platformItmes);
            printToJson(response, result, callback);
        } catch (Exception e) {
            e.printStackTrace();
            Map<String, Object> result = new HashMap<String, Object>();
            result.put("res", "1");
            result.put("msg", e.getMessage());
            printToJson(response, result, callback);
            return;
        }
    }

    @RequestMapping(value = "updatePlatform.do", method = {RequestMethod.GET, RequestMethod.POST})
    public void updatePlatform(HttpServletRequest request, HttpServletResponse response, String platform, String callback) {
        try {
            queryPlatform = platform;
            updateChannelItmes();
            Map<String, Object> result = new HashMap<>();
            result.put("res", "0");
            result.put("serverItmes", serverItmes);
            result.put("channelItmes", channelItmes);
            printToJson(response, result, callback);
        } catch (Exception e) {
            e.printStackTrace();
            Map<String, Object> result = new HashMap<>();
            result.put("res", "1");
            result.put("msg", e.getMessage());
            printToJson(response, result, callback);
            return;
        }
    }

    @Override
    public void search() {
    }

    @Override
    protected String csvContent() {
        return "活跃分析";
    }

    @Override
    protected String csvFileName() {
        return "活跃分析";
    }

    public List<ActiveTendData> getActiveTrendDatas() {
        return activeTrendDatas;
    }

    public void setActiveTrendDatas(List<ActiveTendData> activeTrendDatas) {
        this.activeTrendDatas = activeTrendDatas;
    }

    public ChartUIData getActiveTrendChart() {
        return activeTrendChart;
    }

    public void setActiveTrendChart(ChartUIData activeTrendChart) {
        this.activeTrendChart = activeTrendChart;
    }

    public ChartUIData getActiveTrendChart2() {
        return activeTrendChart2;
    }

    public void setActiveTrendChart2(ChartUIData activeTrendChart2) {
        this.activeTrendChart2 = activeTrendChart2;
    }

    public ChartUIData getActiveTrendChart1() {
        return activeTrendChart1;
    }

    public void setActiveTrendChart1(ChartUIData activeTrendChart1) {
        this.activeTrendChart1 = activeTrendChart1;
    }
}
